﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BusLib
{
    public partial class Stop
    {
        public LatLong Position { get { return new LatLong(stop_lat, stop_lon); } }

        class SameRouteComparer : IEqualityComparer<Stop_Time>
        {
            public bool Equals(Stop_Time a, Stop_Time b) { return a.Trip.Route == b.Trip.Route; }
            public Int32 GetHashCode(Stop_Time a) { return a.Trip.Route.route_id; }
        }

        public List<Stop_Time> GetNextTrips(DateTime startTime)
        {
            TimeSpan startTimespan = startTime - startTime.Date;
            if (startTimespan < TimeSpan.FromHours(4.0)) // Before 4am
               startTimespan.Add(TimeSpan.FromDays(1)); // make 1am be 25 hours, instead of 1hr

            IEnumerable<Stop_Time> stopTimes = Stop_Times.Where(
                st => st.departure_time >= startTimespan)
                .OrderBy(st => st.departure_time);

            stopTimes = stopTimes.Distinct(new SameRouteComparer());

            return stopTimes.ToList();
        }

        public static Stop GetById(int stopId)
        {
            return BusLibDataContext.db.Stops.Single(s => s.stop_id == stopId);
        }

        public static Stop[] FindStopsNear(LatLong pos, double miles)
        {
            double latdist = GeoUtils.OneMileNorth * miles / 2.0f;
            double londist = GeoUtils.OneMileEast * miles / 2.0f;
            return BusLibDataContext.db.Stops.Where(s =>
                   s.stop_lat > pos.latitude - latdist
                && s.stop_lat < pos.latitude + latdist
                && s.stop_lon > pos.longitude - londist
                && s.stop_lon < pos.longitude + londist
                ).ToArray();
        }

        static int submitCount = 0;
        static public Stop ImportCSV(string line)
        {
            Stop stop = null;
            string[] fields = line.Split(',');

            if (fields[0] != "stop_id")
            {
                stop = new Stop();
                stop.stop_id = int.Parse(fields[0]);
                if (fields[1] != "")
                {
                    stop.stop_code = int.Parse(fields[1]);
                }
                stop.stop_name = fields[2];
                stop.stop_desc = fields[3];
                stop.stop_lat = double.Parse(fields[4]);    
                stop.stop_lon = double.Parse(fields[5]);
                stop.zone_id = int.Parse(fields[6]);
                stop.stop_url = fields[7];
                if (fields[8] != "")
                {
                    stop.location_type = int.Parse(fields[8]);
                }
                if (fields[8] != "")
                {
                   stop.parent_station = int.Parse(fields[9]);
                }

                BusLibDataContext.db.Stops.InsertOnSubmit(stop);
                if (submitCount > 1000)
                {
                    BusLibDataContext.db.SubmitChanges();
                    submitCount = 0;
                }
            }
            return stop;
        }
    }
}
